package com.agileai.portal.controller.mobile;

import java.util.Date;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.forum.ForumSectionInfoTreeManage;
import com.agileai.portal.extend.forum.ForumPrivilegeInterceptor;
import com.agileai.util.DateUtil;

public class MobileForumDataProviderHandler extends SimpleHandler {
	private static String GUEST = "GUEST";
	public MobileForumDataProviderHandler(){
		super();
	}
	
	@PageAction
	public ViewRenderer findForumInformations(DataParam param){
		String responseText = null;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			//热点帖子
			List<DataRow> results1 = forumSectionInfoTreeManage.findHotPostRecords();
			JSONObject jsonObject1 = new JSONObject();
			JSONArray jsonArray1 = new JSONArray();
			for(int i=0;i<results1.size();i++){
				DataRow row = results1.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("title", row.stringValue("FPM_TITLE"));
				jsonObject.put("id", row.stringValue("FPM_ID"));
				jsonObject.put("name", row.stringValue("FU_NAME"));
				jsonObject.put("clickNum", row.stringValue("FPM_CLICK_NUMBER"));
				jsonObject.put("createTime", row.stringValue("FPM_CREATE_TIME"));
				jsonObject.put("count", row.stringValue("FRI_ID_COUNT"));
				String type = "";
				if("ESSENCE_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "essence";
				}else if("KEY_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "key";
				}else if("ORDINARY_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "ordinary";
				}
				jsonObject.put("category", row.stringValue("FCI_CODE"));
				jsonObject.put("type", type);
				jsonArray1.put(jsonObject);
			}
			
			jsonObject1.put("hotnewPosts", jsonArray1);
			//集成平台
			String colCode2 = "platform";
			List<DataRow> results2 = forumSectionInfoTreeManage.findPostMessageRecords(colCode2);
			JSONArray jsonArray2 = new JSONArray();
			for(int i=0;i<results2.size();i++){
				DataRow row = results2.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("title", row.stringValue("FPM_TITLE"));
				jsonObject.put("id", row.stringValue("FPM_ID"));
				jsonObject.put("name", row.stringValue("FU_NAME"));
				jsonObject.put("clickNum", row.stringValue("FPM_CLICK_NUMBER"));
				jsonObject.put("createTime", row.stringValue("FPM_CREATE_TIME"));
				jsonObject.put("category", row.stringValue("FCI_CODE"));
				jsonObject.put("top", row.stringValue("FPM_SECTION_TOP"));
				jsonObject.put("count", row.stringValue("FRI_ID_COUNT"));
				String type = "";
				if("ESSENCE_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "essence";
				}else if("KEY_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "key";
				}else if("ORDINARY_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "ordinary";
				}
				jsonObject.put("type", type);
				jsonArray2.put(jsonObject);
			}
			jsonObject1.put("integratePlatform", jsonArray2);
			//支撑平台
			String colCode3 = "support-platform";
			List<DataRow> results3 = forumSectionInfoTreeManage.findPostMessageRecords(colCode3);
			JSONArray jsonArray3 = new JSONArray();
			for(int i=0;i<results3.size();i++){
				DataRow row = results3.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("title", row.stringValue("FPM_TITLE"));
				jsonObject.put("id", row.stringValue("FPM_ID"));
				jsonObject.put("name", row.stringValue("FU_NAME"));
				jsonObject.put("clickNum", row.stringValue("FPM_CLICK_NUMBER"));
				jsonObject.put("createTime", row.stringValue("FPM_CREATE_TIME"));
				jsonObject.put("category", row.stringValue("FCI_CODE"));
				jsonObject.put("top", row.stringValue("FPM_SECTION_TOP"));
				jsonObject.put("count", row.stringValue("FRI_ID_COUNT"));
				String type = "";
				if("ESSENCE_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "essence";
				}else if("KEY_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "key";
				}else if("ORDINARY_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "ordinary";
				}
				jsonObject.put("type", type);
				jsonArray3.put(jsonObject);
			}
			jsonObject1.put("bracePlatform", jsonArray3);
			//应用平台
			String colCode4 = "application-platform";
			List<DataRow> results4 = forumSectionInfoTreeManage.findPostMessageRecords(colCode4);
			JSONArray jsonArray4 = new JSONArray();
			for(int i=0;i<results4.size();i++){
				DataRow row = results4.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("title", row.stringValue("FPM_TITLE"));
				jsonObject.put("id", row.stringValue("FPM_ID"));
				jsonObject.put("name", row.stringValue("FU_NAME"));
				jsonObject.put("clickNum", row.stringValue("FPM_CLICK_NUMBER"));
				jsonObject.put("createTime", row.stringValue("FPM_CREATE_TIME"));
				jsonObject.put("category", row.stringValue("FCI_CODE"));
				jsonObject.put("top", row.stringValue("FPM_SECTION_TOP"));
				jsonObject.put("count", row.stringValue("FRI_ID_COUNT"));
				String type = "";
				if("ESSENCE_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "essence";
				}else if("KEY_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "key";
				}else if("ORDINARY_POSTS".equals(row.stringValue("FPM_TYPE"))){
					type = "ordinary";
				}
				jsonObject.put("type", type);
				jsonArray4.put(jsonObject);
			}
			jsonObject1.put("applicationPlatform", jsonArray4);
			
			responseText = jsonObject1.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer getForumInformation(DataParam param){
		String fpmId = param.getString("fpmId");
		String responseText = null;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			DataRow postInfoRow = forumSectionInfoTreeManage.getForumPostMessageRecord(fpmId);
			User user = (User)this.getUser();
			String userCode = user.getUserCode();
			String fuId = forumSectionInfoTreeManage.getFuIdRecord(userCode);
			int count = forumSectionInfoTreeManage.getForumFavoriteRecord(fpmId, fuId);
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("id", postInfoRow.stringValue("FPM_ID"));
			jsonObject1.put("title", postInfoRow.stringValue("FPM_TITLE"));
			jsonObject1.put("name", postInfoRow.stringValue("FU_NAME"));
			jsonObject1.put("clickNum", postInfoRow.stringValue("FPM_CLICK_NUMBER"));
			jsonObject1.put("createTime", postInfoRow.stringValue("FPM_CREATE_TIME"));
			jsonObject1.put("content", postInfoRow.stringValue("FPM_CONTENT"));
			String type = "";
			if("ESSENCE_POSTS".equals(postInfoRow.stringValue("FPM_TYPE"))){
				type = "essence";
			}else if("KEY_POSTS".equals(postInfoRow.stringValue("FPM_TYPE"))){
				type = "key";
			}else if("ORDINARY_POSTS".equals(postInfoRow.stringValue("FPM_TYPE"))){
				type = "ordinary";
			}
			if(count == 0){
				jsonObject1.put("isAttention", "N");
			}else{
				jsonObject1.put("isAttention", "Y");
			}
			jsonObject1.put("type", type);
			jsonObject1.put("category", postInfoRow.stringValue("FCI_CODE"));
			List<DataRow> replyInfosList = forumSectionInfoTreeManage.findRepliesInfoRecords(fpmId);
			JSONArray jsonArray1 = new JSONArray();
			for(int i=0;i<replyInfosList.size();i++){
				DataRow row = replyInfosList.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("id", row.stringValue("FRI_ID"));
				jsonObject.put("userName", row.stringValue("FU_NAME"));
				jsonObject.put("time", row.stringValue("FRI_TIME"));
				jsonObject.put("floor", i+1);//当前楼数
				jsonObject.put("reportNum", row.stringValue("FRI_REPORT_NUM"));//回复楼数
				jsonObject.put("comment", row.stringValue("FRI_CONTENT"));
				jsonArray1.put(jsonObject);
			}
			jsonObject1.put("comments", jsonArray1);
			responseText = jsonObject1.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer findUserForumPostInfos(DataParam param){
		User user = (User)this.getUser();
		String userCode = user.getUserCode();
		String responseText = null;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			List<DataRow> postInfoList = forumSectionInfoTreeManage.findForumPostInfosByUserCode(userCode);
			JSONObject jsonObject1 = new JSONObject();
			JSONArray jsonArray1 = new JSONArray();
			for(int i=0;i<postInfoList.size();i++){
				DataRow row = postInfoList.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("id", row.stringValue("FPM_ID"));
				jsonObject.put("title", row.stringValue("FPM_TITLE"));
				jsonObject.put("name", row.stringValue("FU_NAME"));
				jsonObject.put("clickNum", row.stringValue("FPM_CLICK_NUMBER"));
				Date date = (Date) row.get("FPM_CREATE_TIME");
				String time = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING, date);
				jsonObject.put("createTime", time.substring(5));
				jsonArray1.put(jsonObject);
			}
			jsonObject1.put("datas", jsonArray1);
			responseText = jsonObject1.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer findUserAttentionForumPostInfos(DataParam param){
		User user = (User)this.getUser();
		String userCode = user.getUserCode();
		String responseText = null;
		try {
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			List<DataRow> postInfoList = forumSectionInfoTreeManage.findUserAttentionForumPostInfos(userCode);
			JSONObject jsonObject1 = new JSONObject();
			JSONArray jsonArray1 = new JSONArray();
			for(int i=0;i<postInfoList.size();i++){
				DataRow row = postInfoList.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("id", row.stringValue("FPM_ID"));
				jsonObject.put("title", row.stringValue("FPM_TITLE"));
				jsonObject.put("name", row.stringValue("FU_NAME"));
				jsonObject.put("clickNum", row.stringValue("FPM_CLICK_NUMBER"));
				Date date = (Date) row.get("FPM_CREATE_TIME");
				String time = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_SLANTING, date);
				jsonObject.put("createTime", time.substring(5));
				jsonArray1.put(jsonObject);
			}
			jsonObject1.put("datas", jsonArray1);
			responseText = jsonObject1.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer mobileIsGuest(DataParam param){
		User user = (User)this.getUser();
		String userCode = user.getUserCode();
		String responseText = null;
		try {
			JSONObject jsonObject = new JSONObject();
			if(GUEST.equals(userCode) || ForumPrivilegeInterceptor.isGuest(user)){
				jsonObject.put("isGuest", true);
			}else{
				jsonObject.put("isGuest", false);
			}
			responseText = jsonObject.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
}
